import request from "../../utils/http";
import { Address } from "../../utils/type";
import { anyObj, promisify, showConfirm, wxBack, wxToast } from "../../utils/util";

// pages/saveAddress/saveAddress.ts
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		query: <anyObj>{},
		addressStr: "",
		address: {
			"tag": "",
			"contacts": "",
			"phone": "",
			"province": "",
			"city": "",
			"district": "",
			"street": "",
			"lng": 0,
			"lat": 0,
			"detailAddress": "",
			"isDefault": 0,
			"outletId": 0
		}		
	},

	async onChangeForm(e: WechatMiniprogram.CustomEvent) {
		console.log("e: ", e)
		const address = this.data.address as any;
		address[e.currentTarget.dataset.key] = e.detail.value;
		console.log("address: ", address)
		this.setData({
			address
		});
	},

	/**
	 * 选择地图
	 */
	async chooseLocation() {
		try {
			const data = await promisify(wx.chooseLocation);
			const address = this.data.address;
			const addressData = await request({
				url: '/api/customers/reservation/getOutlet',
				method: 'POST',
				data: {
					"lat": data.latitude,
					"lng": data.longitude,
				},
				errToast: true,
				showLoading: {title: '正在获取地址数据...'},
				contentTypeForm: true
			});
			if (!addressData) {
				wxToast({icon: 'error', title: '当前暂无网点'});
			}
			address.lat = data.latitude;
			address.lng = data.longitude;
			address.outletId = addressData.id;
			address.province = addressData.province;
			address.city = addressData.city;
			address.district = addressData.district;
			this.setData({
				address,
				addressStr: data.address
			});
		} catch (error) {
			
		}
	},

	async getAddress() {
		try {
			const address: Address = await request({
				url: '/api/customers/address/getOneDetail',
				method: 'GET',
				query: {
					id: this.data.query.addressId
				},
				errToast: true,
				contentTypeForm: true,
			});
			this.setData({
				address,
				addressStr: `${address.province}${address.city}${address.district}${address.street}`
			});
		} catch (error) {
			
		}
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad(query: anyObj) {
		this.setData({query});
		if (query.addressId) {
			this.getAddress();
			wx.setNavigationBarTitle({title: '修改地址'});
		} else {
			wx.setNavigationBarTitle({title: '新增地址'});
		}
	},

	async submit() {
		await showConfirm({content: '确定填写好了?'});
		try {
			const address = this.data.address;
			let addressId = this.data.query.addressId;
			const addressIdData = await request({
				url: '/api/customers/address/save',
				method: 'POST',
				data: {
					...address,
					id: addressId
				},
				contentTypeForm: false,
				errToast: true,
				showLoading: {title: '正在保存地址...'}
			});
			wx.setStorageSync("RESERVATION_ADDRESS", JSON.stringify({
				detailAddress: address.detailAddress,
				contacts: address.contacts,
				phone: address.phone,
				addressId: !addressId ? addressIdData : addressId,
				outletId: address.outletId
			}));
			wxToast({icon: 'success', title: '保存地址成功'});
			wxBack({});
		} catch (error) {
			
		}
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow() {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom() {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage() {

	}
})